package com.otod.pm.comment.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.otod.base.exception.AppException;
import com.otod.base.service.BaseService;
import com.otod.commons.entity.Page;
import com.otod.commons.utils.Collections3;
import com.otod.commons.utils.StringUtils;
import com.otod.pm.comment.mapper.AsmgSubmitInfoMapper;
import com.otod.pm.comment.vo.AsmgSubmitInfoVo;
import com.otod.pm.constants.PmConstant;
import com.otod.pm.prj.service.ProjectService;
import com.otod.pm.prj.service.TaskService;
import com.otod.pm.prj.vo.ProjectVo;
import com.otod.pm.prj.vo.TaskVo;
import com.otod.sys.amcm.MessageConstant;
import com.otod.sys.amcm.service.AmcmMsgAppService;
import com.otod.sys.emp.service.IEmpService;
import com.otod.sys.file.service.impl.FileServiceImpl;
import com.otod.sys.vo.AuapTEmpVo;
import com.otod.sys.vo.FileVo;

/**
 * 模板管理
 * @author jqiu
 */
@Service
@Transactional(readOnly=true)
public class SubmitInfoService extends BaseService {
	
	@Autowired
	AsmgSubmitInfoMapper submitInfoMapper;
	
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private TaskService taskService;
	
	@Autowired
	private FileServiceImpl fileService;
	
	@Autowired
	private IEmpService empService;
	
	@Autowired
	private AmcmMsgAppService msgService;

	/**
	 * 获取项目VO
	 * @param vo
	 */
	public AsmgSubmitInfoVo getById(String id){
		return submitInfoMapper.getById(id);
	}
	
	
	/**
	 * 更新项目
	 */
	@Transactional
	public void updateTp(Map<String,Object> map) {
		submitInfoMapper.update(map);
	}
	
	/**
	 * 新增项目
	 */
	@Transactional
	public AsmgSubmitInfoVo addTp(AsmgSubmitInfoVo vo) {
		vo.setUuid(super.getUUID());
		submitInfoMapper.insert(vo);
		return vo;
	}
	
	/**
	 * 查询项目信息列表  
	 * @param param	参数
	 * @return	分页信息和结果数据
	 */
	public List<AsmgSubmitInfoVo> queryForList(Map<String,Object> param){
		List<AsmgSubmitInfoVo> dataList =  submitInfoMapper.queryForList(param);
		return dataList;
	}
	
	/**
	 * 分页查询项目信息列表
	 * @param page	分页信息
	 * @param param	参数
	 * @return	分页信息和结果数据
	 */
	public Page queryForList(Page page,Map<String,Object> param){
		
		List<AsmgSubmitInfoVo> dataList =  submitInfoMapper.queryForList(page,param);
		for (AsmgSubmitInfoVo asmgSubmitInfoVo : dataList) {
			asmgSubmitInfoVo.setPrjName(projectService.getById(asmgSubmitInfoVo.getPrjId()).getPrjName());
			if(PmConstant.TASK.equals(asmgSubmitInfoVo.getSubmitType())){
				asmgSubmitInfoVo.setShowName(taskService.getById(asmgSubmitInfoVo.getBusitableKey()).getTaskName());
				asmgSubmitInfoVo.setSubmitType("任务审核");
			}else if(PmConstant.FRUIT.equals(asmgSubmitInfoVo.getSubmitType())){
				asmgSubmitInfoVo.setShowName(fileService.getById(asmgSubmitInfoVo.getBusitableKey()).getFileName());
				asmgSubmitInfoVo.setSubmitType("成果审核");
				
			}else if(PmConstant.PROJECT.equals(asmgSubmitInfoVo.getSubmitType())){
				asmgSubmitInfoVo.setShowName(projectService.getById(asmgSubmitInfoVo.getBusitableKey()).getPrjName());
				asmgSubmitInfoVo.setSubmitType("项目审核");
				asmgSubmitInfoVo.setPrjName("");
			}
			
			if(PmConstant.SUCCESS.equals(asmgSubmitInfoVo.getStatus())){
				asmgSubmitInfoVo.setStatus("审核成功");
			}else if(PmConstant.FAILURE.equals(asmgSubmitInfoVo.getStatus())){
				asmgSubmitInfoVo.setStatus("审核失败");
			}else if(PmConstant.SUBMIT.equals(asmgSubmitInfoVo.getStatus())){
				asmgSubmitInfoVo.setStatus("提交申请");
			}
			
			asmgSubmitInfoVo.setSubmitEmpNo(asmgSubmitInfoVo.getCreateEmp());
		}
		page.setAaData(dataList);
		return page;
	}
	
	/**
	 * 删除模板 根据uuid
	 */
	@Transactional
	public void delTp(String id) {
		//删除项目
		submitInfoMapper.deleteById(id);
	}
	
	
	
	/**
	 * 查询全部项目
	 * @param id
	 */
	public List<AsmgSubmitInfoVo> findAll(){
		return submitInfoMapper.queryForList(new HashMap<String, Object>());
	}
	
	
	
	/**
	 * 提交审核
	 * @param id
	 * @throws AppException 
	 */
	@Transactional
	public void submit(AsmgSubmitInfoVo submitInfoVo,String submitEmpNo,Map<String,Object> map) throws AppException{

		if(StringUtils.isEmpty(submitEmpNo)){
			throw new AppException("未选择提交对象，请重新选择！");
		}
		String[] arry = submitEmpNo.split(";");
		
		for (String empNo : arry) {
			
			submitInfoVo.setTargetEmpNo(empNo);
			List<AsmgSubmitInfoVo> list = checkHasSubmit(submitInfoVo);
			TaskVo vo = new TaskVo();
			AuapTEmpVo empVo = empService.findByEmpNo(empNo);
			if(Collections3.isEmpty(list)){
				this.addTp(submitInfoVo);
				
			}else{
				if(!PmConstant.SUBMIT.equals(list.get(0).getStatus())){
					map.put("uuid", list.get(0).getUuid());
					map.put("status", PmConstant.SUBMIT);
					this.updateTp(map);
					
				}else{
					throw new AppException("请求已提交，无需再次提交");
				}
			}
			
			if(PmConstant.FRUIT.equals(submitInfoVo.getSubmitType())){
				
				FileVo fileVo = fileService.getById(submitInfoVo.getBusitableKey());
				vo.setPrjId(submitInfoVo.getPrjId());
				vo.setUuid(submitInfoVo.getBusitableKey());
				vo.setEmpNo(empNo);
				vo.setEmpName(empVo.getEmpName());
				vo.setTaskName(fileVo.getFileName());
				msgService.installMsgVo(vo, MessageConstant.FRUIT_SUBMIT);
			}else if(PmConstant.PROJECT.equals(submitInfoVo.getSubmitType())){
				
				ProjectVo projectrVo = projectService.getById(submitInfoVo.getBusitableKey());
				vo.setPrjId(submitInfoVo.getPrjId());
				vo.setUuid(submitInfoVo.getBusitableKey());
				vo.setEmpNo(empNo);
				vo.setEmpName(empVo.getEmpName());
				vo.setTaskName(projectrVo.getPrjName());
				projectService.changePrjStatus(submitInfoVo.getPrjId(), PmConstant.PRJ_STATUS_SUBMIT);
				msgService.installMsgVo(vo, MessageConstant.PROJECT_SUBMIT);
			}
			
			
		}
	}
	/**
	 * 查询是否已提交审核
	 * @param vo
	 * @return
	 */
	public List<AsmgSubmitInfoVo> checkHasSubmit(AsmgSubmitInfoVo vo){
		Map<String,Object> map =new  HashMap<String,Object>();
		map.put("submitType", vo.getSubmitType());
		map.put("prjId", vo.getPrjId());
		map.put("busitableKey", vo.getBusitableKey());
		map.put("submitEmpNo", vo.getSubmitEmpNo());
		map.put("targetEmpNo", vo.getTargetEmpNo());
		List<AsmgSubmitInfoVo> voList = this.queryForList(map);
		
		return voList;
	}
	
	/**
	 * 开始项目查询 true-审核已通过，false-失败或有上级未审核
	 * @param param	参数
	 * @return	
	 */
	public boolean beginProject(String prjId,String userNo){
		boolean flag = false;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("prjId", prjId);
		map.put("userNo", userNo);
		Integer count =  submitInfoMapper.queryCount(map);
		if(count>0){
			return flag;
		}else{
			flag = true;
			return flag;
		}
	}
	

	
	
}
